import { ref } from 'vue'
import axios from '../plugins/axios'

export default (articleId?: number) => {
   const starList = ref<StarListModel[]>()

   const star = async () => {
      await axios.request({ url: 'star', data: { articleId }, method: 'post' })
   }

   const starInfo = ref<StarModel>()
   const findOneStar = async () => {
      const res = await axios.request<StarModel>({ url: `star/findOne/${articleId}` })
      starInfo.value = res.data
   }

   const cancelStar = async (id: number) => {
      const res = await axios.request<StarModel>({ url: `star/${id}`, method: 'Delete' })
      starList.value?.map((item, index) => {
         if (item.articelId == res.data.articelId) {
            starList.value?.splice(index, 1)
         }
      })
   }

   const getStarList = async () => {
      const res = await axios.request<StarListModel[]>({ url: 'star' })
      starList.value = res.data
   }

   return { star, cancelStar, findOneStar, starInfo, starList, getStarList }
}
